/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.chain.shell;

import java.util.StringTokenizer;
import tal.drivechain.chain.ChainNode;
import tal.drivechain.system.Globals;

/**
 *
 * @author eisental
 */
public class LogObject extends ShellObject {
    private boolean workOnDebug;

    public LogObject(DriveShell shell, boolean workOnDebug) {
        super("debug", null, "debug", shell);
        this.workOnDebug = workOnDebug;
        if (!workOnDebug) {
            this.name = "info";
            this.abb = "info";
        }
    }

    @Override
    public boolean isActionSupported(Action action) {
        return (action == Action.set);
    }

    @Override
    public void set(String command) throws IllegalArgumentException {
        //command: <node name> <on/off>
        StringTokenizer t = new StringTokenizer(command);
        if (t.countTokens()!=2)
            throw new IllegalArgumentException("Invalid syntax. should be: set " + name + " <node-name> <on/off>");

        String nodeName = t.nextToken();
        String state = t.nextToken();

        boolean flag;
        if (state.equals("on")) flag = true;
        else if (state.equals("off")) flag = false;
        else throw new IllegalArgumentException("Invalid syntax. should be: set " + name + " <mod/chain> <name> <on/off>");

        ChainNode n = Globals.findNode(nodeName, shell.getActiveChain());
        if (n==null) throw new IllegalArgumentException("Unknown node instance name: '" + nodeName + "'");

        if (workOnDebug) {
            n.setDebugFlag(flag);
        } else {
            n.setInfoFlag(flag);
        }
        
        out.println("Node " + n.getName() + " (" + n.getClass().getSimpleName() + ") " + " will " + (flag?"now":"not") + " display " + name + " messages");
    }

    @Override
    public void list() throws IllegalArgumentException {
        throw new UnsupportedOperationException("Not supported.");
    }

    @Override
    public void inspect(String command) throws IllegalArgumentException {
        throw new UnsupportedOperationException("Not supported.");
    }

    @Override
    public void make(String command) throws IllegalArgumentException {
        throw new UnsupportedOperationException("Not supported.");
    }

    @Override
    public void remove(String command) throws IllegalArgumentException {
        throw new UnsupportedOperationException("Not supported.");
    }

}
